<?php

namespace app\modules\admingoods\controllers;

use Yii;
use app\components\AdminController;
use yii\data\Pagination;
use common\models\Tools;
use common\models\Goods;

/**
 * Default controller for the `admingoods` module
 */
class DefaultController extends AdminController
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        $userid = Yii::$app->user->identity->id;
        $username = Yii::$app->user->identity->username;
        $region_3 = Yii::$app->user->identity->region_3;
        //获取当前用户的角色
        
        
        $condition = [];
        $page_no = Tools::GetSafeParam('page_no', 1, 0);
        $condition['name'] = Tools::GetSafeParam('name', '', 1);
        $seller = Tools::GetSafeParam('seller', '', 1);
        $seller_id = Tools::GetSafeParam('seller_id', '', 1);
        $condition['first_cate'] = Tools::GetSafeParam('first_cate', 0, 0);
        $condition['second_cate'] = Tools::GetSafeParam('second_cate', 0, 0);
        $condition['third_cate'] = Tools::GetSafeParam('third_cate', 0, 0);
        
        
        $roleid = \common\models\SysUser2role::getUserRoleId($userid);
        switch ($roleid){
            case 1://管理员，查看所有商品
                $condition['seller'] = $seller;
                $condition['seller_id'] = $seller_id;

                $data = Goods::getBackendGoodsList($condition,$page_no);

                $page = Tools::showCmPager($page_no, $data['perpage'], $data['total'],$condition);
                break;
            case 2://区域代理商，查看所属区域的商品
                $condition['region_3'] = $region_3;
                $condition['seller'] = $seller;
                $condition['seller_id'] = $seller_id;
                $data = Goods::getBackendGoodsList($condition,$page_no);

                $page = Tools::showCmPager($page_no, $data['perpage'], $data['total'],$condition);
                break;
            case 3://商家，查看自己发布的商品
                $condition['seller_id'] = $userid;
                
                $condition['seller'] = $username;
                $data = Goods::getBackendGoodsList($condition,$page_no);

                $page = Tools::showCmPager($page_no, $data['perpage'], $data['total'],$condition);
                break;
            default:
                
        }
//        echo "<pre>";
//        print_r($data);exit;
        $category_arr = \common\models\Category::getKeyValue();
//        echo "<pre>";
//        print_r($category_arr);exit;
        $status_arr = array(0=>'待审核',1=>'在售',2=>'下架',3=>'未通过',4=>'已删除');
        return $this->render('index',[
            'data' => $data,
            'page' => $page,
            'condition' => $condition,
            'category_arr' =>$category_arr,
            'status_arr' =>$status_arr,
            ]);
    }
    
    /**
     * 商品审核列表
     * @return string
     */
    public function actionAuthlist()
    {
        $userid = Yii::$app->user->identity->id;
        $username = Yii::$app->user->identity->username;
        $region_3 = Yii::$app->user->identity->region_3;
        //获取当前用户的角色
        
        
        $condition = [];
        $page_no = Tools::GetSafeParam('page_no', 1, 0);
        $condition['status'] = 0;
        
        $roleid = \common\models\SysUser2role::getUserRoleId($userid);
        
        $roleid = \common\models\SysUser2role::getUserRoleId($userid);
        switch ($roleid){
            case 1://管理员，查看所有商品

                $data = Goods::getBackendGoodsList($condition,$page_no);

                $page = Tools::showCmPager($page_no, $data['perpage'], $data['total'],$condition);
                break;
            case 2://区域代理商，查看所属区域的商品
                $condition['region_3'] = $region_3;
                $data = Goods::getBackendGoodsList($condition,$page_no);

                $page = Tools::showCmPager($page_no, $data['perpage'], $data['total'],$condition);
                break;
            default:
                
        }
        
//        print_r($data);exit;
        
//        $data = \common\models\Goods::find()->where(['status'=>0]);  //Field为model层,在控制器刚开始use了field这个model,这儿可以直接写Field,开头大小写都可以,为了规范,我写的是大写
//        $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);    //实例化分页类,带上参数(总条数,每页显示条数)
//        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        
        return $this->render('authlist',[
            'data' => $data,
            'page' => $page,
            'front_server'=>Yii::$app->params['server'],
        ]);
    }
    
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionAuth()
    {
        //检测登录情况
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = '未登录！';
            $output['errorcode'] = 1001;
            Tools::outputjson($output);
        }
        
        $goods_id = Tools::GetSafeParam('goods_id', 0, 0);
        $status = Tools::GetSafeParam('status', -1, 0);
        $reason = Tools::GetSafeParam('reason', '', 1);
        
        if($status==3 && $reason==""){
            $output['success'] = 0;
            $output['msg'] = '请填写未通过原因！';
            Tools::outputjson($output);
        }
        
        if($status == -1){
            $data = \common\models\Goods::getGoodsDetail($goods_id);
            $data['first_cate'] = \common\models\Category::getNameById($data['first_cate']);
            $data['second_cate'] = \common\models\Category::getNameById($data['second_cate']);
            $data['third_cate'] = \common\models\Category::getNameById($data['third_cate']);
            $output['success'] = 1;
            $output['data'] = $data;
            Tools::outputjson($output);
        }
        else{
            $model = Goods::findOne(['id'=>$goods_id]);
            $model->status = $status;
            $model->reason = $reason;
            if($model->save()){
                $output['success'] = 1;
                $output['msg'] = '提交成功！';
                Tools::outputjson($output);
            }
            else{
                var_dump($model->errors);
            }
        }
        //return $this->render('auth');
    }
    
    /***
     * 设置商品状态
     */
    public function actionSetStatus(){
        //检测登录情况
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = '未登录！';
            $output['errorcode'] = 1001;
            Tools::outputjson($output);
        }
        $goods_id = Tools::GetSafeParam('goods_id', 0, 0);
        $status = Tools::GetSafeParam('status', 0, 0);
        $userid = Yii::$app->user->identity->id;
        $region_3 = Yii::$app->user->identity->region_3;
        
        $goods = Goods::findOne($goods_id);
        //权限检测
        $roleid = \common\models\SysUser2role::getUserRoleId($userid);
        switch ($roleid){
            case 1://管理员，查看所有商品
                break;
            case 2://区域代理商，查看所属区域的商品
                if($goods->region_3 != $region_3){
                    $output['success'] = 0;
                    $output['msg'] = '该商品不属于您代理的区域！';
                    $output['errorcode'] = 1002;
                    Tools::outputjson($output);
                }
                break;
            case 3://商家，查看自己发布的商品
                if($goods->seller_id != $userid){
                    $output['success'] = 0;
                    $output['msg'] = '不能操作不属于您的商品！';
                    $output['errorcode'] = 1003;
                    Tools::outputjson($output);
                }
                break;
            default:
        }
        //设置商品状态
        $goods->status = $status;
        if($goods->save()){
            $output['success'] = 1;
            $output['msg'] = '操作成功！';
            Tools::outputjson($output);
        }else{
            $output['success'] = 0;
            $output['msg'] = '系统错误！';
            $output['errorcode'] = 1000;
            Tools::outputjson($output);
        }
    }
}
